<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="文章标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="文章标题"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="删除标志（0代表存在 1代表删除）" prop="deleteStatus">
      <el-input v-model="dataForm.deleteStatus" placeholder="删除标志（0代表存在 1代表删除）"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="文章发布人" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="文章发布人"></el-input>
    </el-form-item>
    <el-form-item label="mongo内容id" prop="contentId">
      <el-input v-model="dataForm.contentId" placeholder="mongo内容id"></el-input>
    </el-form-item>
    <el-form-item label="文章点赞数" prop="likesNumber">
      <el-input v-model="dataForm.likesNumber" placeholder="文章点赞数"></el-input>
    </el-form-item>
    <el-form-item label="书籍封面照片" prop="coverImage">
      <el-input v-model="dataForm.coverImage" placeholder="书籍封面照片"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          title: '',
          updateTime: '',
          deleteStatus: '',
          createTime: '',
          userId: '',
          contentId: '',
          likesNumber: '',
          coverImage: ''
        },
        dataRule: {
          title: [
            { required: true, message: '文章标题不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          deleteStatus: [
            { required: true, message: '删除标志（0代表存在 1代表删除）不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '文章发布人不能为空', trigger: 'blur' }
          ],
          contentId: [
            { required: true, message: 'mongo内容id不能为空', trigger: 'blur' }
          ],
          likesNumber: [
            { required: true, message: '文章点赞数不能为空', trigger: 'blur' }
          ],
          coverImage: [
            { required: true, message: '书籍封面照片不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/member/article/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.title = data.article.title
                this.dataForm.updateTime = data.article.updateTime
                this.dataForm.deleteStatus = data.article.deleteStatus
                this.dataForm.createTime = data.article.createTime
                this.dataForm.userId = data.article.userId
                this.dataForm.contentId = data.article.contentId
                this.dataForm.likesNumber = data.article.likesNumber
                this.dataForm.coverImage = data.article.coverImage
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/member/article/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'title': this.dataForm.title,
                'updateTime': this.dataForm.updateTime,
                'deleteStatus': this.dataForm.deleteStatus,
                'createTime': this.dataForm.createTime,
                'userId': this.dataForm.userId,
                'contentId': this.dataForm.contentId,
                'likesNumber': this.dataForm.likesNumber,
                'coverImage': this.dataForm.coverImage
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
